package com.icode.web.validator;

import com.icode.core.dto.order.OrderDTO;
import com.icode.core.dto.user.CityDTO;
import com.icode.core.dto.user.DistrictDTO;
import com.icode.core.dto.user.ProvinceDTO;
import com.icode.core.model.user.City;
import com.icode.core.model.user.District;
import com.icode.core.model.user.Province;
import com.icode.service.order.OrderService;
import com.icode.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-23
 * Time: 下午1:55
 * To change this template use File | Settings | File Templates.
 */
@Component
public class OrderFormValidator implements Validator {

    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    @Override
    public boolean supports(Class<?> clazz) {
        return OrderDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        OrderDTO orderDTO = (OrderDTO) target;
        String realName = orderDTO.getRealName();
        String IDCard = orderDTO.getIDCard();
        String province = orderDTO.getProvince();
        String city = orderDTO.getCity();
        String district = orderDTO.getDistrict();
        String area = orderDTO.getArea();

        if (!isSelect(province) || !isSelect(city) || !isSelect(district) || "".equals(area)) {
            errors.rejectValue("area", null, "请填写完整地址!");
            orderDTO.setHouseWeekGuid(orderDTO.getHouseWeekGuid());
            OrderDTO o = orderService.createOrder(orderDTO.getHouseWeekGuid(), orderDTO.getPhoneNum());
            String num = orderDTO.getOrderInfoDTO().getNum();
            orderDTO.setOrderInfoDTO(o.getOrderInfoDTO());
            orderDTO.getOrderInfoDTO().setNum(num);
            orderDTO.setProvinces(o.getProvinces());
            if (isSelect(province) && isSelect(city) && isSelect(district)) {
                List<City> cities = userService.findCityById(orderDTO.getProvince());
                List<District> districts = userService.findDistrictById(orderDTO.getCity());
                orderDTO.setCities(CityDTO.toDTOs(cities));
                orderDTO.setDistricts(DistrictDTO.toDTOs(districts));
            }
            return;
        }

        List<Province> provinces = userService.findProvince();
        List<City> cities = userService.findCityById(orderDTO.getProvince());
        List<District> districts = userService.findDistrictById(orderDTO.getCity());
        orderDTO.initAddress(ProvinceDTO.toDTOs(provinces), CityDTO.toDTOs(cities), DistrictDTO.toDTOs(districts));

        if (!Pattern.matches("[\u4E00-\u9FA5]*", realName)) {
            errors.rejectValue("realName", null, "不能有非法字符");
        }
        if (!Pattern.matches("[\\d]{17}[\\w]{1}|[\\d]{14}[\\w]{1}", IDCard)) {
            errors.rejectValue("IDCard", null, "身份证不合法!");
        }

        orderDTO.setHouseWeekGuid(orderDTO.getHouseWeekGuid());
        OrderDTO o = orderService.createOrder(orderDTO.getHouseWeekGuid(), orderDTO.getPhoneNum());
        orderDTO.setHouseInfoDTO(o.getHouseInfoDTO());
        String num = orderDTO.getOrderInfoDTO().getNum();
        orderDTO.setOrderInfoDTO(o.getOrderInfoDTO());
        orderDTO.getOrderInfoDTO().setNum(num);
        orderDTO.setProvinces(o.getProvinces());

    }

    private boolean isSelect(String address) {
        if ("noselect".equals(address) || address == null) {
            return false;
        }
        return true;
    }


}
